/**
 * 
 */
package cc.angis.xiugallery.service;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import cc.angis.xiugallery.account.IAccountManager;
import cc.angis.xiugallery.account.QQWeiboAccountManager;
import cc.angis.xiugallery.account.WeiboAccountManager;
import cc.angis.xiugallery.common.GlobalConst;
import cc.angis.xiugallery.data.PlatFormData;
import cc.angis.xiugallery.db.PostPlatformDBHelper;
import cc.angis.xiugallery.db.TableConstants;
import cc.angis.xiugallery.provider.UserAccountProvider;
import cc.angis.xiugallery.util.NetworkStatus;
import android.os.FileObserver;    
import android.util.Log;    

/**
 * @author user
 * 
 */
public class CommentService extends Service {

	private final IBinder mBinder = new LocalBinder();
	
	private Timer mTimer = new Timer();
	private TimerTask mTask = new TimerTask() {
		public void run() {
			if(NetworkStatus.getNetWorkStatus(getApplicationContext())!=-1)
				getComments();
		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	public class LocalBinder extends Binder {
		public CommentService getService() {
			return CommentService.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		mTimer.schedule(mTask, 10000, 3600000);
		addDefaultAlbum();
	}
	
	private void addDefaultAlbum() {
		if (this.getSharedPreferences(GlobalConst.PREF_NAME, 0).getBoolean(GlobalConst.PREF_DEFAUL_ALBUM, false)){
			
			this.getSharedPreferences(GlobalConst.PREF_NAME, 0).edit().putBoolean(GlobalConst.PREF_DEFAUL_ALBUM, true).commit();
		}
	}

	private void getComments() {
		UserAccountProvider provider = new UserAccountProvider(getApplicationContext());
		provider.initSource();
		
		PostPlatformDBHelper postHelper = new PostPlatformDBHelper(getApplicationContext(), TableConstants.TABLE_POST_PLATFORM);
		try{
			List<PlatFormData> datas = postHelper.getAll();
			IAccountManager accountManager;
			for (PlatFormData data : datas){
				switch(data.getPlatformType()){
					case GlobalConst.AccountType.ACCOUNT_TYPE_WEIBO:
						accountManager = new WeiboAccountManager(getApplicationContext());
						accountManager.retrieveComments(provider.getUserAccount(GlobalConst.AccountType.ACCOUNT_TYPE_WEIBO), data.getPostId(), data.getPlatformId());
						break;
					case GlobalConst.AccountType.ACCOUNT_TYPE_TWITTER:
						break;
					case GlobalConst.AccountType.ACCOUNT_TYPE_QQ:
						accountManager = new QQWeiboAccountManager(getApplicationContext());
						accountManager.retrieveComments(provider.getUserAccount(GlobalConst.AccountType.ACCOUNT_TYPE_QQ), data.getPostId(), data.getPlatformId());
						break;
					default:
						break;
				}
			}
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			postHelper.closeDB();
		}
	}
	
	/**  
	 * sd卡上的目录创建监听器  
	 * @author chroya  
	 *  
	 */    
	public class SDCardListener extends FileObserver {    
	    
	    public SDCardListener(String path) {    
	        /*  
	         * 这种构造方法是默认监听所有事件的,如果使用super(String,int)这种构造方法，  
	         * 则int参数是要监听的事件类型.  
	         */    
	        super(path);    
	    }    
	    
	    @Override    
	    public void onEvent(int event, String path) {           
	        switch(event) {    
		        case FileObserver.DELETE:    
		            break;    
		        default:
		        	break;
	        }    
	    }    
	} 

}
